package com.lastproject.web.bean;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.lastproject.dto.CriteriaDTO;
import com.lastproject.dto.DepartmentDTO;
import com.lastproject.dto.ReportDTO;
import com.lastproject.dto.ReportResultDTO;
import com.lastproject.dto.UserDTO;
import com.lastproject.ejb.dao.DepartmentDAORemote;
import com.lastproject.ejb.dao.ReportDAORemote;
import com.lastproject.ejb.dao.UserDAORemote;
import com.lastproject.ejb.util.ManipulateFieldsReflection;

@ManagedBean
@SessionScoped
public class EditReportBean implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3826856935405374308L;

	@EJB
	ReportDAORemote reportDAO;

	@EJB
	UserDAORemote userDAO;

	@EJB
	DepartmentDAORemote departmentDAO;

	// Report to be created
	ReportDTO editedReport;

	// Table Column Lists
	private List<CriteriaDTO> userColumnList;
	private List<CriteriaDTO> employeeColumnList;
	private List<CriteriaDTO> confidentialEmployeeColumnList;
	private List<CriteriaDTO> traineeColumnList;
	private List<CriteriaDTO> candidateColumnList;

	// Selected Columns for Report
	private List<String> selectedColumns;

	// All positions List
	private List<String> positionList;

	// All departments List
	private List<DepartmentDTO> departmentList;

	// Report Clauses
	private String whereClause;
	private String selClause;
	private String fromClause;
	private String sortClause;

	// Tables where to search Report
	private boolean includeUsers = true;
	private boolean includeEmployees = false;
	private boolean includeTrainees = false;
	private boolean includeCandidates = false;
	private boolean includeConfidentialEmployees = false;

	// Report Results
	private List<ReportResultDTO> reportResultDTO;

	// Error Message
	private String error;
	int editedReportID;

	private boolean columnPhase = false;

	public EditReportBean() {
		editedReport = new ReportDTO();
	}

	@PostConstruct
	public void prepareLists() {
		selectedColumns = new ArrayList<String>();
		positionList = reportDAO.getAllPositions();
		departmentList = departmentDAO.getAll();
		System.out.println("position list size " + positionList.size());
		System.out.println("department list size " + departmentList.size());
		selectedColumns = new ArrayList<String>();
		userColumnList = Arrays.asList(
				new CriteriaDTO("Firstname", "firstname"), new CriteriaDTO(
						"Lastname", "lastname"), new CriteriaDTO(
						"Medical Checkup", "medCheckUpDate"), new CriteriaDTO(
						"Work Email", "workEmail"), new CriteriaDTO(
						"Personal Email", "personalEmail"), new CriteriaDTO(
						"Personal Phone", "personalPhone"), new CriteriaDTO(
						"Position", "position"), new CriteriaDTO(
						"Date of birth", "dob"), new CriteriaDTO(
						"Employment Type", "employmentType"), new CriteriaDTO(
						"Employment Date", "employmentDate"));
		employeeColumnList = Arrays.asList(new CriteriaDTO("Career Level",
				"careerLevel"), new CriteriaDTO("Department", "department"),
				new CriteriaDTO("Work Phone", "workPhone"));
		confidentialEmployeeColumnList = Arrays.asList(new CriteriaDTO(
				"Salary", "salary"),
				new CriteriaDTO("Variable Part", "varPart"));
		traineeColumnList = Arrays.asList(new CriteriaDTO("Salary", "salary"));
		candidateColumnList = Arrays.asList(new CriteriaDTO("Firstname",
				"firstname"), new CriteriaDTO("Lastname", "lastname"),
				new CriteriaDTO("Applying for", "applyingFor"),
				new CriteriaDTO("Domain", "domain"), new CriteriaDTO("Email",
						"email"), new CriteriaDTO("Phone nr", "phoneNr"),
				new CriteriaDTO("Statut", "statut"));
	}

	public List<String> getPositionList() {
		return positionList;
	}

	public List<DepartmentDTO> getDepartmentList() {
		return departmentList;
	}

	public String getError() {
		return error;
	}

	public void setColumnPhase(boolean columnPhase) {
		this.columnPhase = columnPhase;
	}

	public boolean isColumnPhase() {
		return columnPhase;
	}

	public ReportDTO geteditedReport() {
		return editedReport;
	}

	public void seteditedReport(ReportDTO report) {
		this.editedReport = report;
	}

	public List<CriteriaDTO> getUserColumnList() {
		return userColumnList;
	}

	public List<CriteriaDTO> getEmployeeColumnList() {
		return employeeColumnList;
	}

	public List<CriteriaDTO> getConfidentialEmployeeColumnList() {
		return confidentialEmployeeColumnList;
	}

	public List<CriteriaDTO> getTraineeColumnList() {
		return traineeColumnList;
	}

	public List<CriteriaDTO> getCandidateColumnList() {
		return candidateColumnList;
	}

	public List<String> getSelectedColumns() {
		return selectedColumns;
	}

	public void setSelectedColumns() {
		selectedColumns = new ArrayList<String>();
		if (isIncludeUsers())
			for (CriteriaDTO c : userColumnList)
				if (c.isSelected()) {
					selectedColumns.add(c.getColumnName());
				}
		if (isIncludeEmployees())
			for (CriteriaDTO c : employeeColumnList)
				if (c.isSelected())
					selectedColumns.add(c.getColumnName());
		if (isIncludeConfidentialEmployees())
			for (CriteriaDTO c : confidentialEmployeeColumnList)
				if (c.isSelected())
					selectedColumns.add(c.getColumnName());
		if (isIncludeTrainees())
			for (CriteriaDTO c : traineeColumnList)
				if (c.isSelected())
					selectedColumns.add(c.getColumnName());
		if (isIncludeCandidates())
			for (CriteriaDTO c : candidateColumnList)
				if (c.isSelected())
					selectedColumns.add(c.getColumnName());
	}

	public boolean isIncludeUsers() {
		return includeUsers;
	}

	public void setIncludeUsers(boolean includeUsers) {
		this.includeUsers = includeUsers;
		if (this.includeUsers == true)
			this.includeCandidates = false;
		System.out.println("set include users: users candidates"
				+ isIncludeUsers() + isIncludeCandidates());
	}

	public boolean isIncludeEmployees() {
		return includeEmployees;
	}

	public void setIncludeEmployees(boolean includeEmployees) {
		this.includeEmployees = includeEmployees;
		if (this.includeEmployees == true) {
			this.includeCandidates = false;
			this.includeTrainees = false;
		}
	}

	public boolean isIncludeTrainees() {
		return includeTrainees;
	}

	public void setIncludeTrainees(boolean includeTrainees) {
		this.includeTrainees = includeTrainees;
		if (this.includeTrainees == true) {
			this.includeCandidates = false;
			this.includeEmployees = false;
			this.includeConfidentialEmployees = false;
		}
	}

	public boolean isIncludeConfidentialEmployees() {
		return includeConfidentialEmployees;
	}

	public void setIncludeConfidentialEmployees(
			boolean includeConfidentialEmployees) {
		this.includeConfidentialEmployees = includeConfidentialEmployees;
		if (this.includeConfidentialEmployees == true) {
			this.includeCandidates = false;
			this.includeTrainees = false;
		}
	}

	public boolean isIncludeCandidates() {
		return includeCandidates;
	}

	public void setIncludeCandidates(boolean includeCandidates) {
		this.includeCandidates = includeCandidates;
		if (this.includeCandidates == true) {
			this.includeUsers = false;
			this.includeEmployees = false;
			this.includeConfidentialEmployees = false;
			this.includeTrainees = false;
		}
	}

	public String getWhereClause() {
		return whereClause;
	}

	public String getSelClause() {
		return selClause;
	}

	public String getFromClause() {
		return fromClause;
	}

	public String getSortClause() {
		return sortClause;
	}

	public List<ReportResultDTO> getReportResultDTO() {
		return reportResultDTO;
	}

	public void setReportResultDTO(List<ReportResultDTO> reportResultDTO) {
		this.reportResultDTO = reportResultDTO;
	}

	public String sortCondition(String column, String order) {
		this.sortClause = column + " " + order;
		editedReport.setSortClause(sortClause);
		reportResultDTO = reportDAO.getReportResults(editedReport);
		return null;
	}

	public String prepareWhereClause() {
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		whereClause = "";
		if (isIncludeUsers()) {
			for (CriteriaDTO c : userColumnList) {
				if (c.isSelected()) {
					if (!c.getEquals().isEmpty()) {
						whereClause = whereClause + "r." + c.getColumnName()
								+ " = " + "'" + c.getEquals() + "'" + " AND ";
					}
					if (!c.getLike().isEmpty()) {
						whereClause = whereClause + "r." + c.getColumnName()
								+ " LIKE " + "'" + c.getLike() + "%" + "'"
								+ " AND ";
					}
					if (!c.getLessThan().isEmpty()) {
						whereClause = whereClause + "r." + c.getColumnName()
								+ " < " + "'" + c.getLessThan() + "'" + " AND ";
					}
					if (!c.getMoreThan().isEmpty()) {
						whereClause = whereClause + "r." + c.getColumnName()
								+ " > " + "\"" + c.getMoreThan() + "\""
								+ " AND ";
					}
					if (c.getNewerThan() != null) {
						whereClause = whereClause + "r." + c.getColumnName()
								+ " > " + "\"" + df.format(c.getNewerThan())
								+ "\"" + " AND ";
					}
					if (c.getOlderThan() != null) {
						whereClause = whereClause + "r." + c.getColumnName()
								+ " < " + "\"" + df.format(c.getOlderThan())
								+ "\"" + " AND ";
					}
				}
			}
		}

		if (isIncludeEmployees()) {
			for (CriteriaDTO c : employeeColumnList) {
				if (c.isSelected()) {
					if (!c.getEquals().isEmpty()) {
						if (c.getColumnName().equals("department")) {
							whereClause = whereClause + "r.department.id"
									+ " = " + "'" + c.getEquals() + "'"
									+ " AND ";
						} else {
							whereClause = whereClause + "r."
									+ c.getColumnName() + " = " + "'"
									+ c.getEquals() + "'" + " AND ";
						}
					}
					if (!c.getLike().isEmpty()) {
						whereClause = whereClause + "r." + c.getColumnName()
								+ " LIKE " + "'" + c.getLike() + "%" + "'"
								+ " AND ";
					}
					if (!c.getLessThan().isEmpty()) {
						whereClause = whereClause + "r." + c.getColumnName()
								+ " < " + "'" + c.getLessThan() + "'" + " AND ";
					}
					if (!c.getMoreThan().isEmpty()) {
						whereClause = whereClause + "r." + c.getColumnName()
								+ " > " + "'" + c.getMoreThan() + "'" + " AND ";
					}
				}
			}
		}

		if (isIncludeConfidentialEmployees()) {
			for (CriteriaDTO c : confidentialEmployeeColumnList) {
				if (c.isSelected()) {
					if (c.isSelected()) {
						if (!c.getEquals().isEmpty()) {
							whereClause = whereClause + "r."
									+ c.getColumnName() + " = " + "'"
									+ c.getEquals() + "'" + " AND ";
						}
						if (!c.getLike().isEmpty()) {
							whereClause = whereClause + "r."
									+ c.getColumnName() + " LIKE " + "'"
									+ c.getLike() + "%" + "'" + " AND ";
						}
						if (!c.getLessThan().isEmpty()) {
							whereClause = whereClause + "r."
									+ c.getColumnName() + " < " + "'"
									+ c.getLessThan() + "'" + " AND ";
						}
						if (!c.getMoreThan().isEmpty()) {
							whereClause = whereClause + "r."
									+ c.getColumnName() + " > " + "'"
									+ c.getMoreThan() + "'" + " AND ";
						}
					}
				}
			}
		}

		if (isIncludeTrainees()) {
			for (CriteriaDTO c : traineeColumnList) {
				if (c.isSelected()) {
					if (c.isSelected()) {
						if (!c.getEquals().isEmpty()) {
							whereClause = whereClause + "r."
									+ c.getColumnName() + " = " + "'"
									+ c.getEquals() + "'" + " AND ";
						}
						if (!c.getLike().isEmpty()) {
							whereClause = whereClause + "r."
									+ c.getColumnName() + " LIKE " + "'"
									+ c.getLike() + "%" + "'" + " AND ";
						}
						if (!c.getLessThan().isEmpty()) {
							whereClause = whereClause + "r."
									+ c.getColumnName() + " < " + "'"
									+ c.getLessThan() + "'" + " AND ";
						}
						if (!c.getMoreThan().isEmpty()) {
							whereClause = whereClause + "r."
									+ c.getColumnName() + " > " + "'"
									+ c.getMoreThan() + "'" + " AND ";
						}
					}
				}
			}
		}

		if (isIncludeCandidates()) {
			for (CriteriaDTO c : candidateColumnList) {
				if (c.isSelected()) {
					if (c.isSelected()) {
						if (!c.getEquals().isEmpty()) {
							whereClause = whereClause + "r."
									+ c.getColumnName() + " = " + "'"
									+ c.getEquals() + "'" + " AND ";
						}
						if (!c.getLike().isEmpty()) {
							whereClause = whereClause + "r."
									+ c.getColumnName() + " LIKE " + "'"
									+ c.getLike() + "%" + "'" + " AND ";
						}
						if (!c.getLessThan().isEmpty()) {
							whereClause = whereClause + "r."
									+ c.getColumnName() + " < " + "'"
									+ c.getLessThan() + "'" + " AND ";
						}
						if (!c.getMoreThan().isEmpty()) {
							whereClause = whereClause + "r."
									+ c.getColumnName() + " > " + "'"
									+ c.getMoreThan() + "'" + " AND ";
						}
					}
				}
			}
		}

		if (whereClause.isEmpty())
			return null;
		else {
			whereClause = whereClause.substring(0, whereClause.length() - 5);
			return whereClause;
		}
	}

	public String prepareSelClause() {
		selClause = "";
		if (selectedColumns != null) {
			for (String s : selectedColumns)
				selClause = selClause + s + ",";
		}
		System.out.println(selClause);
		if (selClause.isEmpty())
			return "";
		else {
			selClause = selClause.substring(0, selClause.length() - 1);
			return selClause;
		}
	}

	public String prepareFromClause() {
		fromClause = "";
		if (isIncludeConfidentialEmployees())
			fromClause = "ConfidentialEmployee";
		else if (isIncludeEmployees())
			fromClause = "Employee";
		else if (isIncludeTrainees())
			fromClause = "Trainee";
		else if (isIncludeUsers())
			fromClause = "User";
		else if (isIncludeCandidates())
			fromClause = "Candidate";
		return fromClause;
	}

	public String getInitialResults() {

		Date date = new Date();

		if (prepareFromClause().isEmpty())
			return error = "Select at least one employee type";

		setSelectedColumns();

		if (prepareSelClause().isEmpty())
			return error = "Select at least one column";

		editedReport.setDate(date);
		if (isIncludeConfidentialEmployees())
			editedReport.setConfidential(false);

		System.out.println(selectedColumns);

		editedReport.setSelClause(prepareSelClause());
		editedReport.setFromClause(prepareFromClause());
		editedReport.setWhereClause(prepareWhereClause());
		editedReport.setSortClause(sortClause);

		System.out.println("selClause" + getSelClause());
		System.out.println("fromClause" + getFromClause());
		System.out.println("whereClause" + getWhereClause());
		System.out.println("sortClause" + getSortClause());

		reportResultDTO = reportDAO.getReportResults(editedReport);
		columnPhase = true;
		error = "";
		return null;

	}

	public String createReport() {
		ReportDTO newReport = new ReportDTO();
		FacesContext f = FacesContext.getCurrentInstance();
		UserDTO userDTO = (UserDTO) f.getExternalContext().getSessionMap()
				.get("userDTO");
		System.out.println(userDTO.getFirstname());
		newReport.setIdEmployee(userDTO.getId());
		newReport.setConfidential(editedReport.isConfidential());
		newReport.setDate(editedReport.getDate());
		newReport.setFromClause(editedReport.getFromClause());
		newReport.setSelClause(editedReport.getSelClause());
		newReport.setWhereClause(editedReport.getWhereClause());
		newReport.setSortClause(editedReport.getSortClause());
		newReport.setName(editedReport.getName());

		if (reportDAO.findByNameDateEmployee(newReport.getName(),
				newReport.getDate(), newReport.getIdEmployee()) != null)
			return error = "A report with that name was already created today";
		reportDAO.create(newReport);
		return error = "Report Successfully created";

	}

	public void exportToXls() {

		String filename = "reportExport.xls";

		try {
			int colNumber = selectedColumns.size();
			HSSFWorkbook wb = new HSSFWorkbook();
			HSSFSheet sheet = wb.createSheet(filename);

			HSSFRow row = sheet.createRow(0);
			// Creating export file header
			for (int i = 0; i < colNumber; i++) {
				row.createCell(i).setCellValue(selectedColumns.get(i));
			}

			SimpleDateFormat simpleDateFormat = new SimpleDateFormat(
					"yyyy/MM/dd");
			DecimalFormat myFormatter = new DecimalFormat("######.##");

			row.createCell(colNumber + 2).setCellValue(
					"Report Name: " + editedReport.getName());
			row.createCell(colNumber + 3).setCellValue(
					"Report Date: "
							+ simpleDateFormat.format(editedReport.getDate()));

			// Populating table of exported file
			for (int i = 0; i < reportResultDTO.size(); i++) {
				row = sheet.createRow(i + 1);

				// Populating a row
				for (int j = 0; j < selectedColumns.size(); j++) {
					switch (selectedColumns.get(j)) {
					case "firstname":
						row.createCell(j).setCellValue(
								reportResultDTO.get(i).getFirstname());
						break;
					case "lastname":
						row.createCell(j).setCellValue(
								reportResultDTO.get(i).getLastname());
						break;
					case "dob":
						if (reportResultDTO.get(i).getDob() != null) {
							String dobString = simpleDateFormat
									.format(reportResultDTO.get(i).getDob());
							row.createCell(j).setCellValue(dobString);
						} else
							row.createCell(j).setCellValue("");
						break;
					case "personalEmail":
						row.createCell(j).setCellValue(
								reportResultDTO.get(i).getPersonalEmail());
						break;
					case "personalPhone":
						row.createCell(j).setCellValue(
								reportResultDTO.get(i).getPersonalPhone());
						break;
					case "employmentDate":
						if (reportResultDTO.get(i).getEmploymentDate() != null) {
							String employmentDateString = simpleDateFormat
									.format(reportResultDTO.get(i)
											.getEmploymentDate());
							row.createCell(j)
									.setCellValue(employmentDateString);
						} else
							row.createCell(j).setCellValue("");
						break;
					case "medCheckUpDate":
						if (reportResultDTO.get(i).getMedCheckUpDate() != null) {
							String medCheckUpDateString = simpleDateFormat
									.format(reportResultDTO.get(i)
											.getMedCheckUpDate());
							row.createCell(j)
									.setCellValue(medCheckUpDateString);
						} else
							row.createCell(j).setCellValue("");
						break;
					case "employmentType":
						row.createCell(j).setCellValue(
								reportResultDTO.get(i).getEmploymentType());
						break;
					case "position":
						row.createCell(j).setCellValue(
								reportResultDTO.get(i).getPosition());
						break;
					case "workEmail":
						row.createCell(j).setCellValue(
								reportResultDTO.get(i).getWorkEmail());
						break;
					case "workPhone":
						row.createCell(j).setCellValue(
								reportResultDTO.get(i).getWorkPhone());
						break;
					case "careerLevel":
						row.createCell(j).setCellValue(
								reportResultDTO.get(i).getCareerLevel());
						break;
					case "salary":
						if (reportResultDTO.get(i).getSalary() != null) {
							String salaryString = myFormatter
									.format(reportResultDTO.get(i).getSalary());
							row.createCell(j).setCellValue(salaryString);
						} else
							row.createCell(j).setCellValue("");
						break;
					case "varpart":
						if (reportResultDTO.get(i).getVarPart() != null) {
							String varpartString = myFormatter
									.format(reportResultDTO.get(i).getVarPart());
							row.createCell(j).setCellValue(varpartString);
						} else
							row.createCell(j).setCellValue("");
						break;
					case "applyingFor":
						row.createCell(j).setCellValue(
								reportResultDTO.get(i).getApplyingFor());
						break;
					case "domain":
						row.createCell(j).setCellValue(
								reportResultDTO.get(i).getDomain());
						break;
					case "email":
						row.createCell(j).setCellValue(
								reportResultDTO.get(i).getEmail());
						break;
					case "phonenr":
						row.createCell(j).setCellValue(
								reportResultDTO.get(i).getPhonenr());
						break;
					case "statut":
						row.createCell(j).setCellValue(
								reportResultDTO.get(i).getStatut());
						break;
					case "department":
						row.createCell(j).setCellValue(
								reportResultDTO.get(i).getDepartment()
										.getName());
						break;
					}
				} // End of populating each row

			} // End of populating table

			FacesContext context = FacesContext.getCurrentInstance();
			HttpServletResponse res = (HttpServletResponse) context
					.getExternalContext().getResponse();
			res.setContentType("application/vnd.ms-excel");
			res.setHeader("Content-disposition", "attachment;filename="
					+ filename);

			ServletOutputStream out = res.getOutputStream();
			wb.write(out);
			out.flush();
			out.close();
			FacesContext.getCurrentInstance().responseComplete();

		} catch (Exception e) {
			error = "Report could not be exported to XLS";
		}

	} // End of XLS Export method

	public String goBack() {
		System.out.println("go back " + columnPhase);
		error = "";
		this.columnPhase = false;
		return null;

	}

	public String editReport() {
		System.out.println("report ID "
				+ Integer.parseInt(FacesContext.getCurrentInstance()
						.getExternalContext().getRequestParameterMap()
						.get("reportID")));
		editedReportID = Integer.parseInt(FacesContext.getCurrentInstance()
				.getExternalContext().getRequestParameterMap().get("reportID"));
		userColumnList = Arrays.asList(
				new CriteriaDTO("Firstname", "firstname"), new CriteriaDTO(
						"Lastname", "lastname"), new CriteriaDTO(
						"Medical Checkup", "medCheckUpDate"), new CriteriaDTO(
						"Work Email", "workEmail"), new CriteriaDTO(
						"Personal Email", "personalEmail"), new CriteriaDTO(
						"Personal Phone", "personalPhone"), new CriteriaDTO(
						"Position", "position"), new CriteriaDTO(
						"Date of birth", "dob"), new CriteriaDTO(
						"Employment Type", "employmentType"), new CriteriaDTO(
						"Employment Date", "employmentDate"));
		employeeColumnList = Arrays.asList(new CriteriaDTO("Career Level",
				"careerLevel"), new CriteriaDTO("Department", "department"),
				new CriteriaDTO("Work Phone", "workPhone"));
		confidentialEmployeeColumnList = Arrays.asList(new CriteriaDTO(
				"Salary", "salary"),
				new CriteriaDTO("Variable Part", "varPart"));
		traineeColumnList = Arrays.asList(new CriteriaDTO("Salary", "salary"));
		candidateColumnList = Arrays.asList(new CriteriaDTO("Firstname",
				"firstname"), new CriteriaDTO("Lastname", "lastname"),
				new CriteriaDTO("Applying for", "applyingFor"),
				new CriteriaDTO("Domain", "domain"), new CriteriaDTO("Email",
						"email"), new CriteriaDTO("Phone nr", "phoneNr"),
				new CriteriaDTO("Statut", "statut"));
		editedReport = reportDAO.findById(editedReportID);
		String[] columns = editedReport.getSelClause().split(",");
		selectedColumns = Arrays.asList(columns);
		String fromClause = editedReport.getFromClause();

		switch (fromClause) {
		case ("Candidate"): {
			this.includeCandidates = true;
			this.includeConfidentialEmployees = false;
			this.includeEmployees = false;
			this.includeTrainees = false;
			this.includeUsers = false;
			break;
		}
		case ("Trainee"): {
			this.includeCandidates = false;
			this.includeConfidentialEmployees = false;
			this.includeEmployees = false;
			this.includeTrainees = true;
			this.includeUsers = true;
			break;
		}
		case ("ConfidentialEmployee"): {
			this.includeCandidates = false;
			this.includeConfidentialEmployees = true;
			this.includeEmployees = true;
			this.includeTrainees = false;
			this.includeUsers = true;
			break;
		}
		case ("Employee"): {
			this.includeCandidates = false;
			this.includeConfidentialEmployees = true;
			this.includeEmployees = true;
			this.includeTrainees = false;
			this.includeUsers = true;
			break;
		}
		case ("User"): {
			this.includeCandidates = false;
			this.includeConfidentialEmployees = false;
			this.includeEmployees = false;
			this.includeTrainees = false;
			this.includeUsers = true;
			break;
		}
		}

		for (String s : selectedColumns) {
			if (includeCandidates) {
				for (CriteriaDTO c : candidateColumnList)
					if (c.getColumnName().equals(s)) {
						c.setSelected(true);
					}
			} else {
				for (CriteriaDTO c : userColumnList)
					if (c.getColumnName().equals(s)) {
						c.setSelected(true);
					}
				for (CriteriaDTO c : employeeColumnList)
					if (c.getColumnName().equals(s)) {
						c.setSelected(true);
					}
				for (CriteriaDTO c : confidentialEmployeeColumnList)
					if (c.getColumnName().equals(s)) {
						c.setSelected(true);
					}
				for (CriteriaDTO c : traineeColumnList)
					if (c.getColumnName().equals(s)) {
						c.setSelected(true);
					}
			}
		}

		if (editedReport.getWhereClause() != null) {
			DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
			String[] criteriaList = editedReport.getWhereClause()
					.split(" AND ");
			for (String criteria : criteriaList) {
				String[] criteriaElem = criteria.split(" ");
				String columnName;
				String conditionName;
				String conditionValue;
				Date dateConditionValue = null;
				columnName = criteriaElem[0].split("\\.")[1];
				conditionName = criteriaElem[1];
				conditionValue = criteriaElem[2].substring(1,
						criteriaElem[2].length() - 1);
				if (conditionValue.substring(conditionValue.length() - 1)
						.equals("%"))
					conditionValue = criteriaElem[2].substring(1,
							criteriaElem[2].length() - 2);
				try {
					dateConditionValue = formatter.parse(conditionValue);
				} catch (Exception e) {
				}
				if (conditionName.equals("LIKE"))
					conditionName = "like";
				if (conditionName.equals("="))
					conditionName = "equals";
				if (conditionName.equals("<"))
					conditionName = "lessThan";
				if (conditionName.equals(">"))
					conditionName = "moreThan";
				for (CriteriaDTO c : userColumnList) {
					if (c.getColumnName().equals(columnName)
							&& !(columnName.equals("dob")
									|| columnName.equals("medCheckUpDate") || columnName
										.equals("employmentDate"))) {
						c.setSelected(true);
						ManipulateFieldsReflection.set(c, conditionName,
								conditionValue);
					}
					if (c.getColumnName().equals(columnName)
							&& (columnName.equals("dob")
									|| columnName.equals("medCheckUpDate") || columnName
										.equals("employmentDate"))) {
						c.setSelected(true);
						System.out.println("column condition"
								+ c.getColumnName() + c.getNewerThan());
						if (conditionName.equals("lessThan"))
							ManipulateFieldsReflection.set(c, "olderThan",
									dateConditionValue);
						else if (conditionName.equals("moreThan"))
							ManipulateFieldsReflection.set(c, "newerThan",
									dateConditionValue);
					}
				}
				for (CriteriaDTO c : employeeColumnList)
					if (c.getColumnName().equals(columnName)) {
						c.setSelected(true);
						ManipulateFieldsReflection.set(c, conditionName,
								conditionValue);
					}
				for (CriteriaDTO c : confidentialEmployeeColumnList)
					if (c.getColumnName().equals(columnName)) {
						c.setSelected(true);
						ManipulateFieldsReflection.set(c, conditionName,
								conditionValue);
					}
				for (CriteriaDTO c : traineeColumnList)
					if (c.getColumnName().equals(columnName)) {
						c.setSelected(true);
						ManipulateFieldsReflection.set(c, conditionName,
								conditionValue);
					}
				for (CriteriaDTO c : candidateColumnList)
					if (c.getColumnName().equals(columnName)) {
						c.setSelected(true);
						ManipulateFieldsReflection.set(c, conditionName,
								conditionValue);
					}
			}
		}
		return "editReport.xhtml";
	}

	public String header(String col) {
		String s = "";
		for (CriteriaDTO c : userColumnList)
			if (c.getColumnName().equals(col))
				s = c.getColumnLabel();
		for (CriteriaDTO c : employeeColumnList)
			if (c.getColumnName().equals(col))
				s = c.getColumnLabel();
		for (CriteriaDTO c : confidentialEmployeeColumnList)
			if (c.getColumnName().equals(col))
				s = c.getColumnLabel();
		for (CriteriaDTO c : traineeColumnList)
			if (c.getColumnName().equals(col))
				s = c.getColumnLabel();
		for (CriteriaDTO c : candidateColumnList)
			if (c.getColumnName().equals(col))
				s = c.getColumnLabel();
		return s;
	}

}
